﻿using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;

namespace Module_Debug
{
    public partial class Form_codeconvert : Form
    {
        public Form_codeconvert(frmMain Frm)
        {
            MainForm = Frm;
            InitializeComponent();
        }

        frmMain MainForm = null;
        string panelID_path = "";//源文件路径，panelID文件夹路径
        string TARGET_path = "";//目标文件夹路径
        string file_name = "";//用于保存load的文件名字

        private void Form_codeconvert_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmMain.frmain.codeconvert_form = null;
        }

        #region 格式转换，仅需在btn_codeconvert_Click中增加switch的case即可
        private void btn_codeconvert_Click(object sender, EventArgs e)
        {
            ArrayList input_code_list = new ArrayList();//用于保存载入的code，调整格式后的内容
            ArrayList output_code_list = new ArrayList();//用于保存用于输出的code，最后直接write
            string path = System.Environment.CurrentDirectory;//存转换后code路径
            string[] arr = tbx_input.Lines;
            string perline = null;
            int zhushi = 0;
            progressBar2.Value = 0;

            if (arr.Length == 0)
            {
                MessageBox.Show("载入的文件是空的");
                return;
            }
            switch (cbx_input.SelectedIndex)//整理输入格式
            {
                case 0://输入2ks
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            arr[i] = arr[i].ToUpper();
                            perline = arr[i];
                            if (arr[i].Contains("//"))//去掉注释
                            {
                                zhushi = arr[i].IndexOf("//");
                                if (zhushi == 0)
                                {
                                    input_code_list.Add(perline);
                                    continue;
                                }
                                perline = arr[i].Substring(0, zhushi);//去掉注释
                            }
                            else
                            {
                                zhushi = arr[i].Length;
                            }

                            if (arr[i].Contains("FF,FF,FF"))
                            {
                                if (arr[i].Substring(0, 9) == "FF,FF,FF,")
                                {
                                    perline = perline.Replace("FF,FF,FF,", "");
                                    perline = "DELAY" + Convert.ToInt32(perline, 16).ToString();
                                }
                            }
                            else if (!Regex.IsMatch(perline.Replace(",", ""), @"(?i)^[0-9a-z]+$"))
                            {
                                perline = "//" + perline;
                            }
                            perline = perline + arr[i].Substring(zhushi);
                            input_code_list.Add(perline);
                            progressBar2.Value = i * 50 / arr.Length;
                        }
                    }
                    break;
                case 1://输入芯颖
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            arr[i] = arr[i].ToUpper();
                            arr[i] = arr[i].Replace(" ", "");
                            arr[i] = arr[i].Replace("	", "");
                            perline = arr[i];
                            if (arr[i].Contains("//"))//去掉注释
                            {
                                zhushi = arr[i].IndexOf("//");
                                if (zhushi == 0)
                                {
                                    input_code_list.Add(perline);
                                    continue;
                                }
                                perline = arr[i].Substring(0, zhushi);//去掉注释
                            }
                            else
                            {
                                zhushi = arr[i].Length;
                            }


                            if (arr[i].Contains("SENDCMD"))
                            {
                                perline = perline.Replace("\\\\\\\\SENDCMD*", "");
                                perline = perline.Replace("0X", "");
                                string[] aaa = perline.Split(';');
                                if (aaa[1] != "0")
                                {
                                    perline = aaa[0] + "," + aaa[2];
                                }
                                else
                                {
                                    perline = aaa[0];
                                }
                            }
                            else if (arr[i].Contains("DELAY*"))
                            {
                                perline = perline.Replace("\\", "");
                                string[] aaa = perline.Split('*');
                                perline = aaa[0] + aaa[1];
                            }
                            else
                            {
                                perline = "//" + perline;
                            }
                            perline = perline + arr[i].Substring(zhushi);
                            input_code_list.Add(perline);
                            progressBar2.Value = i * 50 / arr.Length;
                        }
                    }
                    break;
                case 2://输入瑞鼎
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            arr[i] = arr[i].ToUpper();
                            if (arr[i].Contains("IC WRITE"))//从IC WRITE开始往下处理，其余部分加注释符号add
                            {
                                while (i < arr.Length && !arr[i].Contains("SSD WRITE"))
                                {
                                    arr[i] = arr[i].ToUpper();
                                    perline = arr[i];
                                    if (arr[i].Contains("//"))//去掉注释
                                    {
                                        zhushi = arr[i].IndexOf("//");
                                        if (zhushi == 0)
                                        {
                                            input_code_list.Add(perline);
                                            i++;
                                            continue;
                                        }
                                        perline = arr[i].Substring(0, zhushi);//去掉注释
                                    }
                                    else
                                    {
                                        zhushi = arr[i].Length;
                                    }
                                    if (arr[i].Contains("["))
                                    {
                                    	if(CB_IIC.Checked == true)
										{
											perline = perline.Replace("[", "IIC_xxbit(03,80,");
										}
										else
										{
											perline = perline.Replace("[", "");
										}
                                        perline = perline.Replace("]", "");
                                        perline = perline.Replace("-", ",");

                                        perline = perline + arr[i].Substring(zhushi);
                                        input_code_list.Add(perline);
                                    }
                                    else if (arr[i].Contains("DELAY"))
                                    {
                                        perline = perline + arr[i + 1];
                                        input_code_list.Add(perline);
                                        i++;
                                    }
                                    else
                                    {
                                        input_code_list.Add("//" + perline);
                                    }
                                    i++;
                                }
                            }
                            else
                            {
                                perline = arr[i];
                                input_code_list.Add("//" + perline);
                            }
                            progressBar2.Value = i * 50 / arr.Length;
                        }
                    }
                    break;
                case 3://输入奕力
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            arr[i] = arr[i].ToUpper();
                            arr[i] = arr[i].Replace(" ", "");
                            arr[i] = arr[i].Replace("	", "");
                            perline = arr[i];
                            if (arr[i].Contains("//"))//去掉注释
                            {
                                zhushi = arr[i].IndexOf("//");
                                if (zhushi == 0)
                                {
                                    input_code_list.Add(perline);
                                    continue;
                                }
                                perline = arr[i].Substring(0, zhushi);//去掉注释
                            }
                            else
                            {
                                zhushi = arr[i].Length;
                            }

                            if (arr[i].Contains("REGISTER"))
                            {
                                perline = perline.Replace("REGISTER,", "");
                                perline = perline.Replace(" ", "");
                                perline = perline.Replace("	", "");
                                perline = perline.Replace("0X", "");
                                perline = perline.Remove(2, 3);
                            }
                            else if (arr[i].Contains("DELAY"))
                            {
                                perline = perline.Replace(",", "");
                            }
                            else
                            {
                                perline = "//" + perline;
                            }
                            perline = perline + arr[i].Substring(zhushi);
                            input_code_list.Add(perline);
                            progressBar2.Value = i * 50 / arr.Length;
                        }
                    }
                    break;
                case 4://输入昇显
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            arr[i] = arr[i].ToUpper();
                            arr[i] = arr[i].Replace("	", " ");
                            arr[i] = arr[i].Replace(" ", ",");
                            arr[i] = arr[i].Replace(",,", ",");
                            perline = arr[i];
                            if (arr[i].Contains("//"))//去掉注释
                            {
                                zhushi = arr[i].IndexOf("//");
                                if (zhushi == 0)
                                {
                                    input_code_list.Add(perline);
                                    continue;
                                }
                                perline = arr[i].Substring(0, zhushi);//去掉注释
                            }
                            else
                            {
                                zhushi = arr[i].Length;
                            }

                            if (perline.Contains("/*SLEEP"))
                            {
                                perline = perline.Replace("/*SLEEP(", "");
                                perline = perline.Replace(")", "");
                                perline = "Delay" + perline;
                            }
                            perline = perline + arr[i].Substring(zhushi);
                            input_code_list.Add(perline);
                            progressBar2.Value = i * 50 / arr.Length;
                        }
                    }
                    break;
                case 5://云英谷
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            arr[i] = arr[i].ToUpper();
                            perline = arr[i];
                            if (arr[i].Contains("//"))//去掉注释
                            {
                                zhushi = arr[i].IndexOf("//");
                                if (zhushi == 0)
                                {
                                    input_code_list.Add(perline);
                                    continue;
                                }
                                perline = arr[i].Substring(0, zhushi);//去掉注释
                            }
                            else
                            {
                                zhushi = arr[i].Length;
                            }

                            if (arr[i].Contains("REGW"))
                            {
                                perline = perline.Replace(" ", "");
                                perline = perline.Replace("0X", ",");
                                perline = perline.Replace("REGW,", "");
                            }
                            else if (arr[i].Contains("DELAY"))
                            {
                                perline = perline.Replace("DELAYMS ", "");
                                perline = "Delay" + perline;

                            }
                            else
                            {
                                perline = "//" + perline;
                            }
                            perline = perline + arr[i].Substring(zhushi);
                            input_code_list.Add(perline);
                            progressBar2.Value = i * 50 / arr.Length;
                        }
                    }
                    break;
                case 6://联咏
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            arr[i] = arr[i].ToUpper();
                            perline = arr[i];
                            if (arr[i].Contains("//"))//去掉注释
                            {
                                zhushi = arr[i].IndexOf("//");
                                if (zhushi == 0)
                                {
                                    input_code_list.Add(perline);
                                    continue;
                                }
                                perline = arr[i].Substring(0, zhushi);//去掉注释
                            }
                            else
                            {
                                zhushi = arr[i].Length;
                            }

                            if (arr[i].Contains("REGW"))
                            {
                                perline = perline.Replace(" ", "");
                                perline = perline.Replace("0X", "");
                                perline = perline.Replace("REGW", "");
                            }
                            else if (arr[i].Contains("DELAY"))
                            {
                                perline = perline.Replace(" ", "");
                            }
                            else
                            {
                                perline = "//" + perline;
                            }
                            perline = perline + arr[i].Substring(zhushi);
                            input_code_list.Add(perline);
                            progressBar2.Value = i * 50 / arr.Length;
                        }
                    }
                    break;
                case 7://集创
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            arr[i] = arr[i].ToUpper();
                            perline = arr[i];
                            if (arr[i].Contains("//"))//去掉注释
                            {
                                zhushi = arr[i].IndexOf("//");
                                if (zhushi == 0)
                                {
                                    input_code_list.Add(perline);
                                    continue;
                                }
                                perline = arr[i].Substring(0, zhushi);//去掉注释
                            }
                            else
                            {
                                zhushi = arr[i].Length;
                            }
                            if (arr[i].Contains("R"))
                            {
                                if (arr[i].Substring(0, 1) == "R")
                                {
                                    perline = perline.Replace("   ", "");
                                    perline = perline.Replace("  ", "");
                                    perline = perline.Replace(" ", ",");
                                    perline = perline.Replace("R", "");
                                    if (perline.Substring(perline.Length - 1, 1) == ",")
                                    {
                                        perline = perline.Substring(0, perline.Length - 1);
                                    }
                                }
                            }
                            else if (arr[i].Contains("DELAY"))
                            {
                                perline = perline.Replace(" ", "");
                            }
                            else
                            {
                                perline = "//" + perline;
                            }
                            perline = perline + arr[i].Substring(zhushi);
                            input_code_list.Add(perline);
                            progressBar2.Value = i * 50 / arr.Length;
                        }
                    }
                    break;
                case 8://佳智彩py
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            arr[i] = arr[i].ToUpper();
                            perline = arr[i];
                            if (arr[i].Contains("#"))//去掉注释
                            {
                                zhushi = arr[i].IndexOf("#");
                                if (zhushi == 0)
                                {
                                    input_code_list.Add("//" + perline.Substring(1));
                                    continue;
                                }
                                perline = arr[i].Substring(0, zhushi);//去掉注释
                                arr[i] = arr[i].Substring(0, zhushi) + "//" + arr[i].Substring(zhushi).Substring(1);
                            }
                            else
                            {
                                zhushi = arr[i].Length;
                            }
                            if (arr[i].Contains("PGCLIENT.WRITEREG('WR_DISPLAY_REG"))
                            {
                                perline = perline.Replace("PGCLIENT.WRITEREG('WR_DISPLAY_REG FF", "");
                                perline = perline.Replace("')", "");
                                perline = perline.Replace(" ", "");
                                string gzcpycode = null;
                                for (int length_gzc = 0; length_gzc < perline.Length; length_gzc = length_gzc + 2)
                                {
                                    gzcpycode += "," + perline.Substring(length_gzc, 2);
                                }
                                perline = gzcpycode.Substring(1);
                            }
                            else if (arr[i].Contains("TIME.SLEEP"))
                            {
                                perline = perline.Replace("TIME.SLEEP(", "");
                                perline = perline.Replace(")", "");
                                perline = "Delay" + (Convert.ToDouble(perline) * 1000).ToString();

                            }
                            else
                            {
                                perline = "//" + perline;
                            }
                            perline = perline + arr[i].Substring(zhushi);
                            input_code_list.Add(perline);
                            progressBar2.Value = i * 50 / arr.Length;
                        }
                    }
                    break;
                case 9://佳智彩pc
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            arr[i] = arr[i].ToUpper();
                            perline = arr[i];
                            if (arr[i].Contains("//"))//去掉注释
                            {
                                zhushi = arr[i].IndexOf("//");
                                if (zhushi == 0)
                                {
                                    input_code_list.Add(perline);
                                    continue;
                                }
                                perline = arr[i].Substring(0, zhushi);//去掉注释
                            }
                            else
                            {
                                zhushi = arr[i].Length;
                            }
                            if (arr[i].Contains("WR_DISPLAY_REG FF"))
                            {
                                perline = perline.Replace("WR_DISPLAY_REG FF", "");
                                perline = perline.Replace(" ", "");
                                string gzcpycode = null;
                                for (int length_gzc = 0; length_gzc < perline.Length; length_gzc = length_gzc + 2)
                                {
                                    gzcpycode += "," + perline.Substring(length_gzc, 2);
                                }
                                perline = gzcpycode.Substring(1);
                            }
                            else if (arr[i].Contains("DELAY"))
                            {
                                perline = perline.Replace(" ", "");

                            }
                            else
                            {
                                perline = "//" + perline;
                            }
                            perline = perline + arr[i].Substring(zhushi);
                            input_code_list.Add(perline);
                            progressBar2.Value = i * 50 / arr.Length;
                        }
                    }
                    break;
                case 10://W6
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            arr[i] = arr[i].ToUpper();
                            perline = arr[i];
                            if (arr[i].Contains("//"))//去掉注释
                            {
                                zhushi = arr[i].IndexOf("//");
                                if (zhushi == 0)
                                {
                                    input_code_list.Add(perline);
                                    continue;
                                }
                                perline = arr[i].Substring(0, zhushi);//去掉注释
                            }
                            else
                            {
                                zhushi = arr[i].Length;
                            }
                            if (arr[i].Contains("REGS.WRITE"))
                            {
                                perline = perline.Replace("REGS.WRITE(0,", "");
                                perline = perline.Replace(" ", "");
                                perline = perline.Replace(")", "");
                                perline = perline.Replace("0X", "");
                                perline = perline.Remove(0, 3);
                            }
                            else if (arr[i].Contains("DELAY,"))
                            {
                                perline = perline.Replace(",", "");
                            }
                            else if (arr[i].Contains("TIME"))
                            {
                                perline = perline.Replace("TIME.", "");
                                perline = perline.Replace(")", "");
                                perline = perline.Replace("(", "");
                            }
                            else
                            {
                                perline = "//" + perline;
                            }
                            perline = perline + arr[i].Substring(zhushi);
                            input_code_list.Add(perline);
                            progressBar2.Value = i * 50 / arr.Length;
                        }
                    }
                    break;
                case 11://精测全脚本
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            arr[i] = arr[i].ToUpper();
                            perline = arr[i];
                            if (arr[i].Contains("--"))//去掉注释
                            {
                                zhushi = arr[i].IndexOf("--");
                                if (zhushi == 0)
                                {
                                    input_code_list.Add("//" + perline.Substring(1));
                                    continue;
                                }
                                perline = arr[i].Substring(0, zhushi);//去掉注释
                                arr[i] = arr[i].Substring(0, zhushi) + "//" + arr[i].Substring(zhushi).Substring(1);
                            }
                            else
                            {
                                zhushi = arr[i].Length;
                            }
                            if (arr[i].Contains("REGS.WRITE("))
                            {
                                perline = perline.Replace("REGS.WRITE(", "");
                                perline = perline.Replace(")", "");
                                perline = perline.Replace(" ", "");
                                perline = perline.Replace("0X", "");
                                perline = perline.Remove(0, 5);
                            }
                            else if (arr[i].Contains("FLOWPAUSE"))
                            {
                                perline = perline.Replace("FLOWPAUSE(", "");
                                perline = perline.Replace(")", "");
                                perline = perline.Replace(";", "");
                                perline = "Delay" + perline;

                            }
                            else if (arr[i].Contains("TIME.DELAY"))
                            {
                                perline = perline.Replace("TIME.DELAY(", "Delay");
                                perline = perline.Replace(")", "");
                            }
                            else
                            {
                                perline = "//" + perline;
                            }
                            perline = perline + arr[i].Substring(zhushi);
                            input_code_list.Add(perline);
                            progressBar2.Value = i * 50 / arr.Length;
                        }
                    }
                    break;
                case 12://精测非全脚本
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            arr[i] = arr[i].ToUpper();
                            perline = arr[i];
                            if (arr[i].Contains("//"))//去掉注释
                            {
                                zhushi = arr[i].IndexOf("//");
                                if (zhushi == 0)
                                {
                                    input_code_list.Add(perline);
                                    continue;
                                }
                                perline = arr[i].Substring(0, zhushi);//去掉注释
                            }
                            else
                            {
                                zhushi = arr[i].Length;
                            }
                            if (arr[i].Contains("R"))
                            {
                                perline = perline.Replace("R", "");
                                perline = perline.Replace(" ", "");
                                string gzcpycode = null;
                                for (int length_gzc = 0; length_gzc < perline.Length; length_gzc = length_gzc + 2)
                                {
                                    gzcpycode += "," + perline.Substring(length_gzc, 2);
                                }
                                perline = gzcpycode.Substring(1);
                            }
                            else if (arr[i].Contains("DELAY,"))
                            {
                                perline = perline.Replace(",", "");
                            }
                            else
                            {
                                perline = "//" + perline;
                            }
                            perline = perline + arr[i].Substring(zhushi);
                            input_code_list.Add(perline);
                            progressBar2.Value = i * 50 / arr.Length;
                        }
                    }
                    break;
				case 13://SPI
					{
                        for (int i = 0; i < arr.Length; i++)
                        {
                            arr[i] = arr[i].ToUpper();
                            perline = arr[i];
                            if (arr[i].Contains("//"))
                            {
                                zhushi = arr[i].IndexOf("//");
                                if (zhushi == 0)
                                {
                                    input_code_list.Add(perline);
                                    continue;
                                }
                                perline = arr[i].Substring(0, zhushi);//去掉注释
                            }
                            else
                            {
                                zhushi = arr[i].Length;
                            }

                            if (arr[i].Contains("WR"))
                            {
                                //perline = perline.Replace(" ", "");
                                if(CB_IIC.Checked == true)//code for 2ks SPI Mode, no include power settings
                                {
		                            perline = perline.Replace("WR 0X", "FF,FF,F9,50,");
		                            perline = perline.Replace(" 0X", ",");
									perline = perline.Insert(14, ",");
									perline = perline.Insert(20, ",00");
								}
								else//code for release
								{
		                            perline = perline.Replace("WR 0X", "wr 0x");
		                            perline = perline.Replace(" 0X", " ");
								}
                            }
                            else
                            {
                                perline = "//" + perline;
                            }
                            perline = perline + arr[i].Substring(zhushi);
                            input_code_list.Add(perline);
                            progressBar2.Value = i * 50 / arr.Length;
                        }
					}
					break;
					
				case 14://DCS
					{
						for (int i = 0; i < arr.Length; i++)
						{
							arr[i] = arr[i].ToUpper();
							perline = arr[i];
							if (arr[i].Contains("/"))
							{
								perline = perline.Replace("/*", "//");
								zhushi = arr[i].IndexOf("/");
								if (zhushi == 0)
								{
									input_code_list.Add(perline);
									continue;
								}
								perline = arr[i].Substring(0, zhushi);//去掉注释
							}
							else
							{
								zhushi = arr[i].Length;
							}

							if (arr[i].Contains("*"))
							{
								zhushi = arr[i].IndexOf("*");
								if (zhushi == 0)
								{
									input_code_list.Add(perline);
									continue;
								}
								perline = arr[i].Substring(0, zhushi);//去掉注释
							}
							else
							{
								zhushi = arr[i].Length;
							}

							if (arr[i].Contains("DCSWR"))
							{
								perline = perline.Replace("DCSWR 0X", "");
								perline = perline.Replace(" 0X", ",");
							}
							else
							{
								perline = "//" + perline;
							}
							perline = perline + arr[i].Substring(zhushi);
							input_code_list.Add(perline);
							progressBar2.Value = i * 50 / arr.Length;
						}
					}
					break;
            }
			
            switch (cbx_output.SelectedIndex)//整理输出格式
            {
                case 0://生成2ks code
                    {
                        for (int j = 0; j < input_code_list.Count; j++)
                        {
                            perline = input_code_list[j].ToString();
                            if (cbx_note.Checked == false)//不保留注释
                            {
                                if (perline.Contains("//"))//去掉注释
                                {
                                    zhushi = perline.IndexOf("//");
                                    if (zhushi == 0)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        perline = perline.Substring(0, zhushi);//去掉注释
                                    }
                                }
                            }
                            output_code_list.Add(perline);
                            progressBar2.Value = 50 + j * 50 / arr.Length;
                        }
                    }
                    break;
                case 1://生成精测全脚本code
                    {
                        for (int j = 0; j < input_code_list.Count; j++)
                        {
                            perline = input_code_list[j].ToString();

                            if (perline.Contains("//"))
                            {
                                zhushi = perline.IndexOf("//");
                                if (zhushi == 0)
                                {
                                    if (cbx_note.Checked == true)
                                    {
                                        perline = "--" + perline.Substring(2);
                                        output_code_list.Add(perline);
                                    }
                                    continue;
                                }
                                else
                                {
                                    perline = perline.Substring(0, zhushi);//去掉注释
                                }
                            }
                            else
                            {
                                zhushi = perline.Length;
                            }

                            if (perline.Contains("DELAY"))
                            {
                                perline = perline.Replace("DELAY", "TIME.DELAY(");
                                perline = perline + ")";
                            }
                            else
                            {
                                perline = "0x" + perline.Replace(",", ",0x");
                                if (perline.LastIndexOf(",") > 3)
                                {
                                    perline = "REGS.WRITE(1,0x39," + perline + ")";
                                }
                                else
                                {
                                    perline = "REGS.WRITE(1,0x05," + perline + ")";
                                }
                            }
                            if (cbx_note.Checked == true)
                            {
                                if (zhushi < input_code_list[j].ToString().Length)
                                {
                                    perline = perline + "--" + input_code_list[j].ToString().Substring(zhushi).Substring(2);
                                }
                            }
                            output_code_list.Add(perline);
                            progressBar2.Value = 50 + j * 50 / arr.Length;
                        }
                    }
                    break;
                case 2://生成精测非全脚本code
                    {
                        for (int j = 0; j < input_code_list.Count; j++)
                        {
                            perline = input_code_list[j].ToString();

                            if (perline.Contains("//"))
                            {
                                zhushi = perline.IndexOf("//");
                                if (zhushi == 0)
                                {
                                    if (cbx_note.Checked == true)
                                    {
                                        perline = "//" + perline.Substring(2);
                                        output_code_list.Add(perline);
                                    }
                                    continue;
                                }
                                else
                                {
                                    perline = perline.Substring(0, zhushi);//去掉注释
                                }
                            }
                            else
                            {
                                zhushi = perline.Length;
                            }

                            if (perline.Contains("DELAY"))
                            {
                                //perline = perline;
                            }
                            else
                            {
                                perline = "R" + perline.Replace(",", " ");
                            }
                            if (cbx_note.Checked == true)
                            {
                                if (zhushi < input_code_list[j].ToString().Length)
                                {
                                    perline = perline + input_code_list[j].ToString().Substring(zhushi);
                                }
                            }
                            output_code_list.Add(perline);
                            progressBar2.Value = 50 + j * 50 / arr.Length;
                        }
                    }
                    break;
                case 3://W6
                    {
                        for (int j = 0; j < input_code_list.Count; j++)
                        {
                            perline = input_code_list[j].ToString();

                            if (perline.Contains("//"))
                            {
                                zhushi = perline.IndexOf("//");
                                if (zhushi == 0)
                                {
                                    if (cbx_note.Checked == true)
                                    {
                                        perline = "//" + perline.Substring(2);
                                        output_code_list.Add(perline);
                                    }
                                    continue;
                                }
                                else
                                {
                                    perline = perline.Substring(0, zhushi);//去掉注释
                                }
                            }
                            else
                            {
                                zhushi = perline.Length;
                            }

                            if (perline.Contains("DELAY"))
                            {
                                perline = perline.Replace("DELAY", "TIME.DELAY(");
                                perline = perline + ")";
                            }
                            else
                            {
                                perline = "0x" + perline.Replace(",", ",0x");
                                if (perline.LastIndexOf(",") > 3)
                                {
                                    perline = "REGS.WRITE(1,0x39," + perline + ")";
                                }
                                else
                                {
                                    perline = "REGS.WRITE(1,0x05," + perline + ")";
                                }
                            }
                            if (cbx_note.Checked == true)
                            {
                                if (zhushi < input_code_list[j].ToString().Length)
                                {
                                    perline = perline + input_code_list[j].ToString().Substring(zhushi);
                                }
                            }
                            output_code_list.Add(perline);
                            progressBar2.Value = 50 + j * 50 / arr.Length;
                        }
                    }
                    break;
                case 4://佳智彩pc
                    {
                        for (int j = 0; j < input_code_list.Count; j++)
                        {
                            perline = input_code_list[j].ToString();
                            if (perline.Contains("//"))
                            {
                                zhushi = perline.IndexOf("//");
                                if (zhushi == 0)
                                {
                                    if (cbx_note.Checked == true)
                                    {
                                        perline = "//" + perline.Substring(2);
                                        output_code_list.Add(perline);
                                    }
                                    continue;
                                }
                                else
                                {
                                    perline = perline.Substring(0, zhushi);//去掉注释
                                }
                            }
                            else
                            {
                                zhushi = perline.Length;
                            }
                            if (perline.Contains("DELAY"))
                            {
                                perline = perline.Replace("DELAY", "DELAY ");
                            }
                            else
                            {
                                string[] aaa = perline.ToString().Split(',');
                                perline = null;
                                perline = "WR_DISPLAY_REG ff " + aaa[0] + " ";
                                for (int b511 = 1; b511 < aaa.Length; b511++)
                                {
                                    perline = perline + aaa[b511];
                                }
                            }
                            if (cbx_note.Checked == true)
                            {
                                if (zhushi < input_code_list[j].ToString().Length)
                                {
                                    perline = perline + input_code_list[j].ToString().Substring(zhushi);
                                }
                            }
                            output_code_list.Add(perline);
                            progressBar2.Value = 50 + j * 50 / arr.Length;
                        }
                    }
                    break;
                case 5://佳智彩py
                    {
                        for (int j = 0; j < input_code_list.Count; j++)
                        {
                            perline = input_code_list[j].ToString();
                            if (perline.Contains("//"))
                            {
                                zhushi = perline.IndexOf("//");
                                if (zhushi == 0)
                                {
                                    if (cbx_note.Checked == true)
                                    {
                                        perline = "#" + perline.Substring(2);
                                        output_code_list.Add(perline);
                                    }
                                    continue;
                                }
                                else
                                {
                                    perline = perline.Substring(0, zhushi);//去掉注释
                                }
                            }
                            else
                            {
                                zhushi = perline.Length;
                            }
                            if (perline.Contains("DELAY"))
                            {
                                perline = perline.Replace("DELAY", "");
                                perline = "time.sleep(" + (Convert.ToDouble(perline) / 1000).ToString() + ")";
                            }
                            else
                            {
                                string[] aaa = perline.ToString().Split(',');
                                perline = null;
                                perline = "WR_DISPLAY_REG ff " + aaa[0] + " ";
                                for (int b511 = 1; b511 < aaa.Length; b511++)
                                {
                                    perline = perline + aaa[b511];
                                }
                                perline = "PGclient.writeReg('" + perline + "')";
                            }
                            if (cbx_note.Checked == true)
                            {
                                if (zhushi < input_code_list[j].ToString().Length)
                                {
                                    perline = perline + "#" + input_code_list[j].ToString().Substring(zhushi).Substring(2);
                                }
                            }
                            output_code_list.Add(perline);
                            progressBar2.Value = 50 + j * 50 / arr.Length;
                        }
                    }
                    break;
                case 6://回读
                    {
                        for (int j = 0; j < input_code_list.Count; j++)
                        {
                            perline = input_code_list[j].ToString();

                            if (perline.Contains("//"))
                            {
                                zhushi = perline.IndexOf("//");
                                if (zhushi == 0)
                                {
                                    if (cbx_note.Checked == true)
                                    {
                                        perline = "//" + perline.Substring(2);
                                        output_code_list.Add(perline);
                                    }
                                    continue;
                                }
                                else
                                {
                                    perline = perline.Substring(0, zhushi);//去掉注释
                                }
                            }
                            else
                            {
                                zhushi = perline.Length;
                            }

                            if (perline.Contains("DELAY"))
                            {
                            }
                            else if (perline.Contains(textBox6.Text.ToUpper()))
                            {
                                perline = perline.Insert(0, "IC WRITE\r\n");
                                perline = perline + "XXX";
                                perline = perline.Replace("XXX", "\r\n" + "IC READ");
                            }
                            else if (Regex.IsMatch(perline.Replace(",", ""), @"(?i)^[0-9a-z]+$"))
                            {
                                string[] lines = perline.Split(',');
                                perline = lines[0] + "," + (lines.Length - 1).ToString();
                            }

                            if (cbx_note.Checked == true)
                            {
                                if (zhushi < input_code_list[j].ToString().Length)
                                {
                                    perline = perline + input_code_list[j].ToString().Substring(zhushi);
                                }
                            }
                            output_code_list.Add(perline);
                            progressBar2.Value = 50 + j * 50 / arr.Length;
                        }
                    }
                    break;
                case 7://安卓
                    {
                        for (int j = 0; j < input_code_list.Count; j++)
                        {
                            perline = input_code_list[j].ToString();
                            if (perline.Contains("//"))
                            {
                                zhushi = perline.IndexOf("//");
                                if (zhushi == 0)
                                {
                                    if (cbx_note.Checked == true)
                                    {
                                        perline = "//" + perline.Substring(2);
                                        output_code_list.Add(perline);
                                    }
                                    continue;
                                }
                                else
                                {
                                    perline = perline.Substring(0, zhushi);//去掉注释
                                }
                            }
                            else
                            {
                                zhushi = perline.Length;
                            }
                            if (perline.Contains("DELAY"))
                            {
                                //忽略delay
                            }
                            else
                            {
                                string[] aaa = perline.ToString().Split(',');
                                string bbb = perline.Replace(',', ',');
                                perline = null;
                                if (aaa.Length == 1)
                                {
                                    perline = "05,00,01," + aaa[0];
                                }
                                else if(aaa.Length>1)
                                {
                                    perline = "39,00," + aaa.Length.ToString("X2")+","+bbb;
                                }
                                else
                                {
                                    MessageBox.Show("行数"+j,ToString()+"有异常");
                                }
                            }
                            if (cbx_note.Checked == true)
                            {
                                if (zhushi < input_code_list[j].ToString().Length)
                                {
                                    perline = perline + "//" + input_code_list[j].ToString().Substring(zhushi).Substring(2);
                                }
                            }
                            output_code_list.Add(perline);
                            progressBar2.Value = 50 + j * 50 / arr.Length;
                        }
                    }
                    break;

            }
            ///////////保存转换后code
            string[] arrString = (string[])output_code_list.ToArray(typeof(string));
            tbx_output.Text = null;
            perline = null;
            for (int i = 0; i < arrString.Length; i++)
            {
                if (i == arrString.Length - 1)
                {
                    perline = perline + arrString[i];
                }
                else
                {
                    perline = perline + arrString[i] + "\r\n";
                }
            }
            tbx_output.Text = perline;
            progressBar2.Value = 100;
            MessageBox.Show("转换完成");
        }
        private void btn_loadcode_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "所有文件(*.*)|*.*|文本文件(*.txt)|*.txt";
            of.FileName = tbx_codepath.Text;
            string code_array = null;
            if (of.ShowDialog() == DialogResult.OK)
            {
                tbx_input.Clear();
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("UTF-8"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string str_generate = streamReader.ReadLine();
                //循环读取文本信息
                while (str_generate != null)
                {
                    str_generate = str_generate.ToUpper();
                    code_array += str_generate + "\r\n";
                    str_generate = streamReader.ReadLine(); //循环读取下一行
                }
                tbx_input.Text = code_array;
                fs.Close(); //关闭流
                streamReader.Close();

            }
            else
            {
                MessageBox.Show("没选择有效路径");
            }
            tbx_codepath.Text = of.FileName;
            file_name = of.SafeFileName;
        }
        private void btn_savecode_Click(object sender, EventArgs e)//保存转换后ccode
        {
            //创建保存对话框对象
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "Code files (*.txt)|*.txt"; //过滤文件类型
            save.FilterIndex = 1;
            save.RestoreDirectory = true;
            if (save.ShowDialog() == DialogResult.OK)
            {
                string path = save.FileName;
                save.DefaultExt = ".txt";
                StreamWriter sw = new StreamWriter(path);
                sw.WriteLine(tbx_output.Text);
                sw.Close();
            }
        }
        private void tbx_codepath_KeyDown(object sender, KeyEventArgs e)
        {
            string code_array = null;
            if (e.KeyCode == Keys.Enter)
            {
                tbx_input.Clear();
                //读取文本信息
                if (!File.Exists(tbx_codepath.Text))
                {
                    MessageBox.Show("路径不存在");
                    return;
                }
                FileStream fs = new FileStream(tbx_codepath.Text, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string str_generate = streamReader.ReadLine();
                //循环读取文本信息
                while (str_generate != null)
                {
                    str_generate = str_generate.ToUpper();
                    code_array += str_generate + "\r\n";
                    str_generate = streamReader.ReadLine(); //循环读取下一行
                }
                tbx_input.Text = code_array;
                fs.Close(); //关闭流
                streamReader.Close();
            }
        }
        private void btn_run_Click(object sender, EventArgs e)//移动到点屏code
        {
            string aaa = frmMain.frmain.txtSourcecode.Text;
            frmMain.frmain.txtSourcecode.Text = tbx_output.Text;
            frmMain.frmain.btnAddSourcecode_Click(null, null);
            frmMain.frmain.txtSourcecode.Text = aaa;
        }
        private void btn_copyoutput_Click(object sender, EventArgs e)//全选复制
        {
            tbx_output.SelectAll();
            tbx_output.Copy();
        }
        #endregion

        #region 复制文件
        private void button4_Click(object sender, EventArgs e)
        {
            string[] folder_filter = textBox2.Text.Split(',');//保存想要复制的文件夹类型
            string[] file_filter = textBox3.Text.Split(',');//保存想要复制的文件类型
            checkedListBox1.SelectedIndex = 0;
            int checked_count = 0;//保存所有被勾选的项的数量
            int copy_count = 0;//保存当前已复制完成的项的数量
            progressBar1.Value = 0;
            button4.Text = "正在拷贝";
            for (int x = 0; x < checkedListBox1.Items.Count; x++)
            {
                if (checkedListBox1.GetItemChecked(x))
                {
                    checked_count++;
                }
            }
            try
            {
                for (int i = 0; i < checkedListBox1.Items.Count; i++)
                {
                    checkedListBox1.SelectedIndex = i;
                    if (checkedListBox1.GetItemChecked(i))//获取第I项的勾选状态
                    {
                        if (checkBox2.Checked == true)//复制文件夹
                        {
                            for (int j = 0; j < folder_filter.Length; j++)
                            {
                                string CSV_path = panelID_path + "\\" + checkedListBox1.SelectedItem.ToString() + "\\" + folder_filter[j];
                                string TARGET_CSV_path = TARGET_path + "\\" + checkedListBox1.SelectedItem.ToString() + "\\" + folder_filter[j];
                                if (Directory.Exists(CSV_path))
                                {
                                    if (!Directory.Exists(TARGET_CSV_path))
                                    {
                                        Directory.CreateDirectory(TARGET_CSV_path);
                                    }
                                    string[] files = Directory.GetFiles(CSV_path);
                                    foreach (string file in files)
                                    {
                                        string pFilePath = TARGET_CSV_path + "\\" + Path.GetFileName(file);
                                        if (File.Exists(pFilePath))
                                            continue;
                                        File.Copy(file, pFilePath, true);
                                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                                    }
                                }
                                else
                                {
                                    MessageBox.Show(checkedListBox1.SelectedItem + "目录下没有" + folder_filter[j] + "文件夹，请检查");
                                }
                            }
                        }
                        if (checkBox1.Checked == true)//复制文件
                        {
                            for (int k = 0; k < file_filter.Length; k++)
                            {
                                string other_path = panelID_path + "\\" + checkedListBox1.SelectedItem.ToString();
                                string TARGET_other_path = TARGET_path + "\\" + checkedListBox1.SelectedItem.ToString();
                                if (Directory.Exists(other_path))
                                {
                                    if (!Directory.Exists(TARGET_other_path))
                                    {
                                        Directory.CreateDirectory(TARGET_other_path);
                                    }
                                    string imgtype = "*." + file_filter[k];//过滤文件类型
                                    string[] files = Directory.GetFiles(other_path, imgtype);
                                    foreach (string file in files)
                                    {
                                        string pFilePath = TARGET_other_path + "\\" + Path.GetFileName(file);
                                        if (File.Exists(pFilePath))
                                            continue;
                                        File.Copy(file, pFilePath, true);
                                    }
                                }
                                else
                                {
                                    MessageBox.Show(checkedListBox1.SelectedItem + "目录下没有" + file_filter[k] + "文件，请检查");
                                }
                            }

                        }
                        copy_count++;
                        progressBar1.Value = copy_count * 100 / checked_count;
                        checkedListBox1.SetItemChecked(i, false);
                    }

                }
                button4.Text = "拷贝完成";
                MessageBox.Show("文件复制完成", "春晖说");
            }
            catch
            {
                MessageBox.Show("复制过程出错，请重试", "春晖说");
            }

        }
        private void button1_Click(object sender, EventArgs e)
        {
            checkedListBox1.Items.Clear();
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.SelectedPath = panelID_path;
            if (path.ShowDialog() == DialogResult.OK)
            {
                textBox4.Text = path.SelectedPath;
                DirectoryInfo folder = new DirectoryInfo(path.SelectedPath);
                DirectoryInfo[] dics = folder.GetDirectories();//获取path路径下所有文件夹集合
                Array.Sort(dics, delegate (DirectoryInfo x, DirectoryInfo y) { return y.CreationTime.CompareTo(x.CreationTime); });//将文件夹名称集合按文件夹修改时间排序
                for (int i = 0; i < dics.Length; i++)
                {
                    this.checkedListBox1.Items.Add(dics[i]);//将文件夹的集合循环添加到listbox中
                }
                panelID_path = path.SelectedPath;
            }
            else
            {
                MessageBox.Show("没选择任何路径");
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();//载入目标路径
            if (path.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = path.SelectedPath;
                TARGET_path = textBox1.Text;
                button4.Enabled = true;
            }
            else
            {
                MessageBox.Show("没选择任何路径");
            }
        }
        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                checkedListBox1.Items.Clear();
                if (!Directory.Exists(textBox4.Text))
                {
                    MessageBox.Show("路径不存在");
                    return;
                }
                DirectoryInfo folder = new DirectoryInfo(textBox4.Text);
                DirectoryInfo[] dics = folder.GetDirectories();//获取path路径下所有文件夹集合
                Array.Sort(dics, delegate (DirectoryInfo x, DirectoryInfo y) { return y.CreationTime.CompareTo(x.CreationTime); });//将文件夹名称集合按文件夹修改时间排序
                for (int i = 0; i < dics.Length; i++)
                {
                    this.checkedListBox1.Items.Add(dics[i]);//将文件夹的集合循环添加到listbox中
                }
                panelID_path = textBox4.Text;
            }
        }
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!Directory.Exists(textBox1.Text))
                {
                    MessageBox.Show("目标路径不存在");
                    return;
                }
                else
                {
                    TARGET_path = textBox1.Text;
                    button4.Enabled = true;
                }
            }
        }










        #endregion

        private void button_pps_load_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "所有文件(*.*)|*.*|文本文件(*.txt)|*.txt";
            of.FileName = textBox_pps_path.Text;
            string code_array = null;
            if (of.ShowDialog() == DialogResult.OK)
            {
                textBox_input_pps.Clear();
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("UTF-8"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string str_generate = streamReader.ReadLine();
                //循环读取文本信息
                while (str_generate != null)
                {
                    str_generate = str_generate.ToUpper();
                    code_array += str_generate + "\r\n";
                    str_generate = streamReader.ReadLine(); //循环读取下一行
                }
                textBox_input_pps.Text = code_array;
                fs.Close(); //关闭流
                streamReader.Close();
            }
            else
            {
                MessageBox.Show("没选择有效路径");
            }
            textBox_pps_path.Text = of.FileName;
            file_name = of.SafeFileName;
        }

        private void button_pps_convert_Click(object sender, EventArgs e)
        {
            ArrayList input_code_list = new ArrayList();    //
            ArrayList output_code_list = new ArrayList();   //
            string perline = null;
            string perline2 = null;
            string[] arr = textBox_input_pps.Lines;
            int zhushi = 0;
            if (arr.Length == 0)
            {
                MessageBox.Show("载入的文件是空的");
                return;
            }

			for (int i = 0; i < arr.Length-1; i++)
			{
				arr[i] = arr[i].ToUpper();
				arr[i] = arr[i].Replace("	", " ");
				arr[i] = arr[i].Replace(" ", ",");
				arr[i] = arr[i].Replace(",,", ",");
				perline = arr[i];

				string[] str = perline.Split(',');
				string tmp = "";
				for (int j = 13; j < str.Length-4; )
				{
				
					tmp = str[j];
					str[j] = str[j+3];
					str[j+3] = tmp;

					tmp = str[j+1];
					str[j+1] = str[j+2];
					str[j+2] = tmp;

					j = j + 4;
				}

				for (int k = 13; k < str.Length; k++)
				{
					str[k] = str[k] + ",";
					perline2 = perline2 + str[k];
				}

				input_code_list.Add(perline2);
			}

			for (int m = 0; m < input_code_list.Count; m++)
			{
 				output_code_list.Add(perline2);
 			}
            string[] arrString = (string[])output_code_list.ToArray(typeof(string));
            textBox_output_pps.Text = null;
            perline = null;
            for (int q = 0; q < arrString.Length; q++)
            {
                if (q == arrString.Length - 1)
                {
                    perline = perline + arrString[q];
                }
                else
                {
                    perline = perline + arrString[q] + "\r\n";
                }
            }
            textBox_output_pps.Text = perline;

         }

        private void button_pps_save_Click(object sender, EventArgs e)
        {
            
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "Code files (*.txt)|*.txt"; 
            save.FilterIndex = 1;
            save.RestoreDirectory = true;
            if (save.ShowDialog() == DialogResult.OK)
            {
                string path = save.FileName;
                save.DefaultExt = ".txt";
                StreamWriter sw = new StreamWriter(path);
                sw.WriteLine(textBox_output_pps.Text);
                sw.Close();
            }
        }
    }
}
